#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import datetime
import os
import lib
import logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import datastore_errors
from google.appengine.ext.webapp import template
from lib import llhandler
from lib.workflowManager import WorkflowManager
from model.models import *
class LoginHandler(llhandler.PSHandler):
	
	def internal_get(self):
		
		self.set_flash("Welcome")
		possible_users = PSUser.all().filter('company = ',self.current_company)
		self.add("possible_users",possible_users)
		self.render('login_index')
		#self.render('index')
		#self.base_auth()
		#self.get_internal()
		#user_logout = users.create_logout_url("/eventos/")
		#self.response.out.write("<a href=\"%s\">Logout</a>." %user_logout)
	
	def internal_post(self):
		self.internal_get()

class LoginAsHandler(llhandler.PSHandler):
	
	def get(self,user_key):
	  self.auth_check()
	  self.internal_get(user_key)
	  
	def internal_get(self,user_key):
	  new_user = PSUser.get(db.Key(encoded=user_key))
	  self.user = new_user
	  self.current_user = self.user
	  self.session['current_user'] = self.user
	  self.redirect('/')

def main():
  application = webapp.WSGIApplication([('/login/', LoginHandler),
					('/login/as/(\S+)',LoginAsHandler)
  
					  ],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
